<template>
    <div class="container-common homework-publish">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>作业发布与互评管理</el-breadcrumb-item>
            <el-breadcrumb-item>布置作业</el-breadcrumb-item>
        </el-breadcrumb>
        <el-form ref="form" :model="form" label-width="140px">
            <el-form-item label="作业描述">
                <el-input v-model="form.desc"></el-input>
            </el-form-item>
            <el-form-item label="作业要求">
                <el-input type="textarea" rows="5" v-model="form.request"></el-input>
            </el-form-item>
            <el-form-item label="作业附件">
                <!--<el-input type="textarea"  v-model="form.attachment"></el-input>-->
                <el-upload
                        action="http://39.105.144.7:5002/api/upload"
                        :data="otherUploadData"
                        name="file"
                        :on-success="handleUploadSuccess"
                        :file-list="fileList">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
                </el-upload>
            </el-form-item>
            <el-form-item label="作业人数限制">
                <el-input v-model="form.peopleLimit"></el-input>
            </el-form-item>
            <el-form-item label="作业起止时间">
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.homeworkStart" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.homeworkEnd" style="width: 100%;"></el-date-picker>
                </el-col>
            </el-form-item>
            <el-form-item label="是否分组">
                <el-select v-model="form.isGroup">
                    <el-option label="是" :value="true"></el-option>
                    <el-option label="否" :value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="分组人数设置">
                <el-col :span="3">最多</el-col>
                <el-col :span="8">
                    <el-input v-model="form.groupSettingMax"></el-input>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="3">最少</el-col>
                <el-col :span="8">
                    <el-input v-model="form.groupSettingMin"></el-input>
                </el-col>
            </el-form-item>
            <el-form-item label="是否互评">
                <el-select v-model="form.isRevaluation">
                    <el-option label="是" :value="true"></el-option>
                    <el-option label="否" :value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="互评起止时间">
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.revaluationStart" style="width: 100%;"></el-date-picker>
                </el-col>
                <el-col class="line" :span="2">-</el-col>
                <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.revaluationEnd" style="width: 100%;"></el-date-picker>
                </el-col>
            </el-form-item>
            <el-form-item label="互评评审者数量">
                <el-input v-model="form.revaluationNumber"></el-input>
            </el-form-item>
            <el-form-item label="互评计算方法">
                <el-radio-group v-model="form.revaluationMethod">
                    <el-radio label="去掉最高分和最高分求平均值"></el-radio>
                    <el-radio label="求平均值"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="互评评分细则" class="revaluation-detail">
                <UE ref="revaluationDetail"></UE>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">发布作业</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import Api from '@utils/api';
    import { mapGetters } from 'vuex';
    import UE from '@components/UE.vue';

    export default {
        name: "publish",
        data () {
            return {
                form: {
                    desc: '',
                    request: '',
                    attachment: '',
                    peopleLimit: '',
                    homeworkStart: '',
                    homeworkEnd: '',
                    isGroup: true,
                    groupSettingMax: '',
                    groupSettingMin: '',
                    isRevaluation: true,
                    revaluationStart: '',
                    revaluationEnd: '',
                    revaluationNumber: '',
                    revaluationMethod: ''
                },
                fileList: []
            }
        },
        computed: {
            ...mapGetters([
                'getCurrentCourseId'
            ]),
            otherUploadData () {
                return {
                    type: 'task',
                    id: ''
                }
            }
        },
        components: {
            UE
        },
        methods: {
            onSubmit() {
                Api({
                    method: 'post',
                    url: '/newTask',
                    data: {
                        course_id: this.getCurrentCourseId,
                        task_name: this.form.desc,
                        task_content: this.form.request,
                        task_file: this.form.attachment,
                        start_time: this.form.homeworkStart,
                        expire_time: this.form.homeworkEnd,
                        if_group: this.form.isGroup,
                        max_group_num: this.form.peopleLimit,
                        max_num_per_group: this.form.groupSettingMax,
                        min_num_per_group: this.form.groupSettingMin,
                        if_huping: this.form.isRevaluation,
                        huping_num: this.form.revaluationNumber,
                        huping_start_time: this.form.revaluationStart,
                        huping_expire_time: this.form.revaluationEnd,
                        huping_percent: 0,
                        huping_method: this.form.revaluationMethod,
                        huping_requirement: this.$refs.revaluationDetail.content
                    }
                }).then(() => {

                    this.$notify({
                        type: 'success',
                        title: '新建成功！'
                    });
                })
            },
            handleUploadSuccess (response) {
                if (response.code === 200 && response.data) {
                    this.form.attachment = response.data.id;
                }
                this.fileList = [response.data.filename];
            }
        }
    }
</script>

<style type="text/scss" lang="scss">
    .homework-publish {
        .el-form-item {
            width: 460px;
        }
        .revaluation-detail {
            width: 800px;
        }
        .ueditor-container .ql-editor {
            height: 200px;
            min-height: 200px;
        }
        .line {
            text-align: center;
        }
    }
</style>